#!/usr/bin/perl -w
########################################################################
# script name:  info_hosts.pl
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/02/25    0.1     MDUMONT         initial
########################################################################
use strict;
my $type="host";

my $hostname = `/bin/uname -n`; chomp $hostname;
my $hostid = `/usr/sbin/hostid`; chomp $hostid;
my $date = `/bin/date +"%Y-%m-%d %H:%M:%S"`; chomp $date;
my @TMP;                                                                                                                    

my $outfile = "/var/OPM/data/${hostname}_${type}.out";
open (OUT, ">$outfile");
print OUT '<uname -n>',"\n",$hostname,"\n",'</uname -n>', "\n";
print OUT '<hostid>',"\n",$hostid,"\n",'</hostid>', "\n";
print OUT '<date>',"\n",$date,"\n",'</date>', "\n";

my %CONNECTIONS;
my %HOSTFILE;
my @a_ports_sorted;
########################################################################
# Sub's
########################################################################
sub DnsName {
   my $ip = shift;
   my $lookup = `/usr/bin/nslookup $ip 2>&1 | sed 's/ //g' | grep Name | cut -d':' -f2`; chomp $lookup;
   my $iname = "-";
   my $idomain = "-";
	$iname = $lookup; $iname =~ s/\..*//;
	$idomain = $lookup; $idomain =~ s/.*?\.//;
   return ($iname,$idomain);
}
sub OpenPorts {
   my @netstat = `/bin/netstat -na`;
   my @port;
   foreach my $l (@netstat){
      if ($l =~ /^.+:(\d+)\s+.+LISTEN/){
         push(@port, $1);
      }
   }
	##################
	# Sort it and uniq
	##################
   my %h_ports;
   @h_ports{@port} = ();
   @a_ports_sorted = sort keys %h_ports;
}
sub LoadHostFile {
	my @L = `/bin/cat /etc/hosts`;
	foreach my $l (@L){
		chomp $l;
		$l =~ s/\s+/ /g;
		if    ($l =~ /^(\d+\.\d+\.\d+\.\d+)\s+(.+?)\s+.*#(.*)/){ $HOSTFILE{$1} = "$2\t$3"; }
		elsif ($l =~ /^(\d+\.\d+\.\d+\.\d+)\s+(.+)/){ $HOSTFILE{$1} = "$2\t-"; }
	}
}
########################################################################
# MAIN
########################################################################
# Pour extraire les info pour info_host
{
	print OUT "<host>\n";
	#my $s_prtconf = `/usr/sbin/prtconf 2> /dev/null`;
	my ($model,$serial,$ptype,$cputype,$pnumber,$pclock,$lparnumber) = qw(- - - - - - -);
	my ($lparname,$memory,$hostname,$ipaddr,$netmask,$dnsserver,$domain) = qw(- - - - - - - -);
	#$memory =~ s/(\d+).*/$1/;
	my $tmp = `vmstat | grep System | cut -d' ' -f3,4 2> /dev/null`;
	if ( $tmp =~ /^lcpu=(.*) mem=(.*)MB$/ ){ $pnumber = $1; $memory = $2; }
	$memory = $memory/1024;
	$tmp = `uname -L 2> /dev/null`;
	if ( $tmp =~ /^(.*) (.*)$/ ){ $lparnumber = $1; $lparname = $2; }
	$tmp = `lsattr -E -l proc0 2> /dev/null`;
	if ( $tmp =~ /type\s+(.*) Processor/ ){ $cputype = $1; }
	$tmp = `pmcycles 2> /dev/null`;
	if ( $tmp =~ /This machine runs at (.*) MHz/ ){ $pclock = $1; }
	#my $netactive = `lsdev -Cc adapter | grep Ethernet | grep Available | wc -l | sed 's/ *//g'`; chomp $netactive;
	#my $netinactive = `lsdev -Cc adapter | grep Ethernet | grep -v Available | wc -l | sed 's/ *//g'`; chomp $netinactive;
	my $fcsan	= '0';
	my $fcnotsan	= '0';
	my $scsi	= `lsdev -Cc adapter | grep -i scsi | wc -l | sed 's/ *//g'`;chomp $scsi;
	my $san		= `lsdev -Cc adapter | grep -i fiber | wc -l | sed 's/ *//g'`;chomp $san;
	my $platform = `/bin/uname -p`; chomp $platform;
	`ssh -V > /tmp/sshversion 2>&1`;
	my $ssh = `/bin/cat /tmp/sshversion`;chomp $ssh;if ( $ssh =~ /(.*),.*/ ){ $ssh = $1; }
	my $os = "AIX";
	my $kernel = `/usr/bin/oslevel -s`;chomp $kernel;
	#my $landscape = `cat /etc/BA.conf.conf | grep LAND | head -1 | cut -d' ' -f1 | cut -d'=' -f2`; chomp $landscape;
	my $landscape = `. /etc/BA.conf && echo \$LAND`; chomp $landscape;
	#print OUT "$landscape\t$memory\t$pnumber\t$os\t$kernel\t$cputype\t$ptype\t$platform\t$san\t$netactive\t$netinactive\t$fcsan\t$fcnotsan\t$scsi\t$ssh\n";
	print OUT "$landscape\t$memory\t$pnumber\t$os\t$kernel\t$cputype\t$ptype\t$platform\t$san\t$fcsan\t$fcnotsan\t$scsi\t$ssh\n";
	print OUT "</host>\n";
}
{
	print OUT "<routes>\n";
	my @TMP = `/bin/netstat -nr`; 
	shift @TMP;shift @TMP;shift @TMP;shift @TMP;pop @TMP;pop @TMP;pop @TMP;
	foreach my $route (@TMP){
		my ($dest,$gw,$flags,$ref,$use,$i) = qw(- - - - - -);
		($dest,$gw,$flags,$ref,$use,$i) = split /\s+/, $route;
		print OUT "$dest\t$gw\t$i\n";
	}
	print OUT "</routes>\n";
}
{
	print OUT "<ip>\n";
	OpenPorts();
	LoadHostFile();
	my $tmp = `/etc/ifconfig -a`; $tmp =~ s/^\t+//g; $tmp =~ s/ +/ /g;
	my @interfaces = split /.*\n\n/, $tmp;
	foreach my $eth (@interfaces){
		next if ( $eth =~ /^lo/ );
		my ($ifname,$inet,$netmask,$broadcast,$ether,$listeners)=('NA','NA','NA','NA','NA','NA');
		my @LINES = split /\n/, $eth;
		if ($LINES[0] =~ /^([\w]+):\s/){$ifname = $1;}
		if ($LINES[0] =~ /HWaddr\s([\w:]+)\s/){$ether = $1;}
		if ($LINES[1] =~ /inet\s([\w\.]+)\s/){$inet = $1;}
		if ($LINES[1] =~ /netmask\s([^\s]+)/){$netmask = $1;}
		if ($LINES[1] =~ /broadcast\s([^\s]+)/){$broadcast = $1;}
		my $gateway = `netstat -rn`; $gateway =~ s/ +/ /g;
		if ( $gateway =~ /default (.*) UG/ ){ $gateway = $1; }
		#my $gateway = `netstat -rn | grep default | awk '{print $2}'`; 
		chomp $gateway;
		my ($name,$domain) = DnsName($inet);
		my $info = "";
		my $ifbase = $ifname; $ifbase =~ s/:\d+$//;
		if ($ifname =~ /.*:\d+/){$info .= ",VIP";} else {$info .= ",PHY";};
		if ($hostname eq $name){$info .= ",HOST-IP";}
		$info =~ s/^,//;
		print OUT "$ifname\t$inet\t$gateway\t$netmask\t$broadcast\t$ether\t$info\n";
	}
	print OUT "</ip>\n";
}
{
   print OUT "<process>\n";
   my @PS = `/bin/ps -eo user,args | sort | uniq`;
   foreach my $PS (@PS){
		$PS =~ s/\s+/ /g;
		$PS =~ s/^\s+//g;
      chomp $PS;
      my @a_PS = split / /, $PS;
      my $user = shift(@a_PS);
      #my $command = join(" ", @a_PS);
      print OUT "$user\t@a_PS\n";
   }
   print OUT "</process>\n";
}
close OUT;
`chown saunix $outfile`;
